Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(traverse)!: remove unsound APIs #7514

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Nov 27, 2024

It's essential to oxc_traverse's safety scheme that the user cannot create a TraverseAncestry, because they could then substitute it for the one stored in TraverseCtx, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a TraverseAncestry also requires that user cannot obtain an owned TraverseCtx either, because you can obtain an owned TraverseAncestry from an owned TraverseCtx.

Therefore, it's unsound for TraverseCtx::new to be public.

However, it is useful in minifier to be able to re-use the same TraverseCtx over and over, which requires having an owned TraverseCtx.

To support this use case, introduce ReusableTraverseCtx. It is an opaque wrapper around TraverseCtx, which prevents accessing the TraverseCtx inside it. It's safe for user to own a ReusableTraverseCtx, because there's nothing they can do with it except for using it to traverse via traverse_mut_with_ctx, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for ReusableTraverseCtx.But in the meantime, this keeps Traverse's API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an Traverse visitor, and then mem::swap the 2 x &mut TraverseCtxs. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB by accident, which was the risk before.

Copy link

graphite-app bot commented Nov 27, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added A-minifier Area - Minifier A-transformer Area - Transformer / Transpiler labels Nov 27, 2024
Copy link
Contributor Author

overlookmotel commented Nov 27, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added the C-bug Category - Bug label Nov 27, 2024
Copy link

codspeed-hq bot commented Nov 27, 2024

CodSpeed Performance Report

Merging #7514 will not alter performance

Comparing 11-18-fix_traverse_remove_unsound_apis (5143bb8) with main (6e48059)

Summary

✅ 30 untouched benchmarks

@overlookmotel overlookmotel force-pushed the 11-18-fix_traverse_remove_unsound_apis branch from d21c0db to 28a8277 Compare November 27, 2024 17:29
@overlookmotel overlookmotel marked this pull request as ready for review November 27, 2024 17:33
@overlookmotel
Copy link
Contributor Author

I wrote almost all this last week. Just finishing it up and getting it into git now before my computer dies!

@overlookmotel overlookmotel requested a review from Boshen November 27, 2024 17:48
@overlookmotel
Copy link
Contributor Author

@Boshen It's a bit annoying having to have ReusableTraverseCtx. But can we accept this in the hope that later on we can alter the minifier to remove the need for it, and get rid of it again?

There are alternatives which would also be safe, but they'd be less performant and require more boilerplate code.

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Nov 28, 2024
Copy link

graphite-app bot commented Nov 28, 2024

Merge activity

Boshen pushed a commit that referenced this pull request Nov 28, 2024
It's essential to `oxc_traverse`'s safety scheme that the user cannot create a `TraverseAncestry`, because they could then substitute it for the one stored in `TraverseCtx`, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a `TraverseAncestry` also requires that user cannot obtain an owned `TraverseCtx` either, because you can obtain an owned `TraverseAncestry` from an owned `TraverseCtx`.

Therefore, it's unsound for `TraverseCtx::new` to be public.

However, it is useful in minifier to be able to re-use the same `TraverseCtx` over and over, which requires having an owned `TraverseCtx`.

To support this use case, introduce `ReusableTraverseCtx`. It is an opaque wrapper around `TraverseCtx`, which prevents accessing the `TraverseCtx` inside it. It's safe for user to own a `ReusableTraverseCtx`, because there's nothing they can do with it except for using it to traverse via `traverse_mut_with_ctx`, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for `ReusableTraverseCtx`.But in the meantime, this keeps `Traverse`'s API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an `Traverse` visitor, and then `mem::swap` the 2 x `&mut TraverseCtx`s. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB *by accident*, which was the risk before.
@Boshen Boshen force-pushed the 11-18-fix_traverse_remove_unsound_apis branch from 28a8277 to 84e1bf6 Compare November 28, 2024 01:18
It's essential to `oxc_traverse`'s safety scheme that the user cannot create a `TraverseAncestry`, because they could then substitute it for the one stored in `TraverseCtx`, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a `TraverseAncestry` also requires that user cannot obtain an owned `TraverseCtx` either, because you can obtain an owned `TraverseAncestry` from an owned `TraverseCtx`.

Therefore, it's unsound for `TraverseCtx::new` to be public.

However, it is useful in minifier to be able to re-use the same `TraverseCtx` over and over, which requires having an owned `TraverseCtx`.

To support this use case, introduce `ReusableTraverseCtx`. It is an opaque wrapper around `TraverseCtx`, which prevents accessing the `TraverseCtx` inside it. It's safe for user to own a `ReusableTraverseCtx`, because there's nothing they can do with it except for using it to traverse via `traverse_mut_with_ctx`, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for `ReusableTraverseCtx`.But in the meantime, this keeps `Traverse`'s API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an `Traverse` visitor, and then `mem::swap` the 2 x `&mut TraverseCtx`s. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB *by accident*, which was the risk before.
@Boshen Boshen force-pushed the 11-18-fix_traverse_remove_unsound_apis branch from 84e1bf6 to 5143bb8 Compare November 28, 2024 01:20
@graphite-app graphite-app bot merged commit 5143bb8 into main Nov 28, 2024
25 checks passed
@graphite-app graphite-app bot deleted the 11-18-fix_traverse_remove_unsound_apis branch November 28, 2024 01:27
Boshen pushed a commit that referenced this pull request Nov 28, 2024
It's essential to `oxc_traverse`'s safety scheme that the user cannot create a `TraverseAncestry`, because they could then substitute it for the one stored in `TraverseCtx`, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a `TraverseAncestry` also requires that user cannot obtain an owned `TraverseCtx` either, because you can obtain an owned `TraverseAncestry` from an owned `TraverseCtx`.

Therefore, it's unsound for `TraverseCtx::new` to be public.

However, it is useful in minifier to be able to re-use the same `TraverseCtx` over and over, which requires having an owned `TraverseCtx`.

To support this use case, introduce `ReusableTraverseCtx`. It is an opaque wrapper around `TraverseCtx`, which prevents accessing the `TraverseCtx` inside it. It's safe for user to own a `ReusableTraverseCtx`, because there's nothing they can do with it except for using it to traverse via `traverse_mut_with_ctx`, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for `ReusableTraverseCtx`.But in the meantime, this keeps `Traverse`'s API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an `Traverse` visitor, and then `mem::swap` the 2 x `&mut TraverseCtx`s. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB *by accident*, which was the risk before.
@oxc-bot oxc-bot mentioned this pull request Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-minifier Area - Minifier A-transformer Area - Transformer / Transpiler C-bug Category - Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant